package com.zq.it.dao.impl.exam;

import com.zq.it.dao.common.PageConfig;
import com.zq.it.dao.exam.PageDAO;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.Page;
import com.zq.it.pojo.exam.data.ObjectData;

/**
 * accountDAO的实现类
 *
 * @author 王凡
 *
 * @version $ID: TAccountDAOImpl.java 2013-3-21 王凡@$
 */

public class PageDAOImpl extends BaseDao<Page> implements PageDAO {

	@Override
	public ObjectData getData(PageConfig config) {
		ObjectData data = new ObjectData();
		Long count = (Long) getHibernateTemplate().find(config.getCountHql())
				.listIterator().next();
		int pageSize = config.getPageSize();
		long pageCount = count % pageSize > 0
				? (count / pageSize) + 1
				: count / pageSize;

		int page = config.getPage();
		String hql = config.getHql();

		if (pageSize * (page - 1) >= count) { // 判断选择的页面数据量是否超过数据总量
			page = (int) pageCount;
		}
		if (page < 1) {
			page = 1;
		}
		StringBuffer url = new StringBuffer();
		url.append(config.getUrl());
		if (null != config.getExtendUrl()
				&& !"".equals(config.getExtendUrl())) {
			url.append("?" + config.getExtendUrl() + "&");
		} else {
			url.append("?");
		}
		data.setList(this.findByPage((page - 1) * pageSize, pageSize, hql));
		if (null == data.getList() || data.getList().size() == 0) {
			data.setStr(
					"<div class=\"showPage\"><font class=\"errorField\">没有相关数据！</font></div>");
		} else {
			data.setStr(getPageStr(page, pageCount, pageSize, url.toString(),
					config.getPerPageCount()));
		}
		data.setTotalDataCount(count);
		return data;
	}

	private String getPageStr(int page, long pageCount, int pageSize,
			String url, int perPageCount) {
		StringBuffer str = new StringBuffer("");
		str.append("<div class=\"showPage\">")
				.append("<table cellpadding=\"0\">").append("<tr><td>")
				.append("<a href=\"" + url.toString()
						+ "page=1\">&nbsp;首&nbsp;页&nbsp;</a>")
				.append("</td>");
		if (page != 1) {
			str.append("<td>").append("<a href=\"" + url.toString() + "page="
					+ (page - 1) + "\">&nbsp;上一页&nbsp;</a>").append("</td>");
		}
		if (page < perPageCount) {
			long j = perPageCount > pageCount ? pageCount : perPageCount;
			for (int i = 0; i < j; i++) {
				str.append("<td>").append("<a href=\"" + url.toString()
						+ "page=" + (i + 1) + "\"");
				if (page == i + 1) {
					str.append(" class=\"active\"");
				}
				str.append(">" + (i + 1) + "</a>").append("</td>");
			}
		} else if (pageCount - perPageCount < page - 1) {
			for (int i = 0; i < perPageCount; i++) {
				str.append("<td>").append("<a href=\"" + url.toString()
						+ "page=" + (pageCount - perPageCount + i + 1) + "\"");
				if (page == pageCount - perPageCount + i + 1) {
					str.append(" class=\"active\"");
				}
				str.append(">" + (pageCount - perPageCount + i + 1) + "</a>")
						.append("</td>");
			}
		} else {
			for (int i = 0; i < perPageCount; i++) {
				str.append("<td>").append("<a href=\"" + url.toString()
						+ "page=" + (page + i - perPageCount / 2) + "\"");
				if (page == page + i - perPageCount / 2) {
					str.append(" class=\"active\"");
				}
				str.append(">" + (page + i - perPageCount / 2) + "</a>")
						.append("</td>");
			}
		}
		if (page != pageCount) {
			str.append("<td>").append("<a href=\"" + url.toString() + "page="
					+ (page + 1) + "\">&nbsp;下一页&nbsp;</a>").append("</td>");
		}
		str.append("<td>").append("<a href=\"" + url.toString() + "page="
				+ pageCount + "\">&nbsp;尾&nbsp;页&nbsp;</a>").append("</td>");
		str.append("<td>")
				.append("<input type=\"text\" id=\"count\" class=\"jump-size\" maxlength=\"9\" name=\"page\"/>/<span>"
						+ pageCount + "</span>")
				.append("</td>");
		str.append("<td>").append("<a href=\"javascript:jump('" + url.toString()
				+ "');\">&nbsp;跳转&nbsp;</a>").append("</td>");
		str.append("</tr></table></div>");

		return str.toString();
	}
}